package com.corner.presenter.imp;

import android.content.Intent;

import com.alibaba.fastjson.JSONObject;
import com.corner.R;
import com.corner.app.App;
import com.corner.app.ConsTants;
import com.corner.app.GlobalParams;
import com.corner.model.leancloud.helper.UpdateHelper;
import com.corner.presenter.ISplashPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.utils.ApkUtil;
import com.corner.utils.FileUtil;
import com.corner.utils.NotificationUtil;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Administrator on 2015/7/15，22.16.
 * 描述：
 */
public class SplashPresenter extends BasePresenter implements ISplashPresenter {
    @Override
    public void checkUpdate() {
        UpdateHelper.getUpdate().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<JSONObject>() {
            @Override
            public void call(JSONObject avObject) {
                String version = UpdateHelper.getUpdateVersion(avObject);
                if (!GlobalParams.appversion.equals(version)) {
                    // 版本有更新
                    if (FileUtil.isSDCardAvaliable()) {
                        File file = new File(GlobalParams.APPFIEPATH + "/"
                                + ConsTants.APK_NAME);
                        if (file.exists()) {
                            String appVersion = ApkUtil.getApkVersion(
                                    App.application,
                                    file.getAbsolutePath());
                            if (appVersion != null
                                    && appVersion.equals(version)) {
                                showInstallNotice(file);
                                // 显示更新，不用往下走
                            } else {
                                // 存在，并且不匹配的话，删除之
                                file.delete();
                            }
                        }
                    }
                    String appUrl = UpdateHelper.getUpdateAppUrl(avObject);
                    showUpdateInfo(version, appUrl);
                }
            }
        });
    }

    /**
     * 提示安装
     *
     * @param file
     */
    private void showInstallNotice(File file) {
        // 已经下载了,提示安装
        Intent intent = ApkUtil.getInstallIntent(App.application,
                file.getAbsolutePath());
        NotificationUtil.showNotificationWithActivityIntent(
                App.application, R.drawable.ic_launcher,
                R.string.new_app_download, R.string.click_update, intent, ConsTants.UPDATE_NOTIFY);
    }

    /**
     * 显示更新提示
     *
     * @param version
     */
    private void showUpdateInfo(String version, String avFileUrl) {
        Intent intent = new Intent(ConsTants.ACTION_UPDATE_DOWNSERVICE);
        intent.putExtra(ConsTants.INTENT_FILE_PATH, GlobalParams.APPFIEPATH
                + "/" + ConsTants.APK_NAME);
        intent.putExtra(ConsTants.INTENT_FILE_URl, avFileUrl);
        // 有版本更新 用notification推送消息
        String updateContent = StringUtils.replaceEach(
                App.application.getString(R.string.update_notification_content), new String[]{
                        "VERSION", "LASTVERSION"}, new String[]{
                        GlobalParams.appversion, version});
        NotificationUtil.showNotificationWithServiceIntent(App.application,
                R.drawable.ic_launcher,
                App.application.getString(R.string.update_notification_title), updateContent,
                intent, ConsTants.UPDATE_NOTIFY);
    }
}
